|||##########################################################################
|||## A Macroquest2 Casino written by Grimshad & Killians
|||## casinolotto.mac - Handles all lotto code
|||##########################################################################
|||---------------------------------------------------------------------------------------------------------Lotto Events
#event Lotto "#1# #*# 'Lotto'"
#event Playlotto "#1# #*# 'Play Lotto'"
#event Tickets "#1# #*# 'Tickets'"
#event Claim "#1# says, 'Claim'"
#event Previous "#1# #*# 'Previous'"
#event Current "#1# #*# 'Current'"
#event Playlotto "#1# #*# 'Play Lotto'"
#event BuyTickets "#1# #*# 'buy #2#"
|||---------------------------------------------------------------------------------------------------------Lotto Initialization
Sub LottoInit
	/if (${Debug}) /echo in LottoInit
	/declare DealerName string outer ${Me.CleanName}
	/declare LottoPlayer string outer 0
	/declare TicketCount string outer 0
	/return
|||---------------------------------------------------------------------------------------------------------Lotto Info
Sub Event_Lotto(Line, Player)
	/if (${Debug}) /echo in Lotto(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/delay ${TellDelay}
	/tell ${Player} Every day a 100k lotto is drawn, for every ticket you own the odds are 1:2000. Each ticket only costs 100pp and every day the winning number will be announced in ooc. Check the [Previous] winner or check the [Current] winner. If you win, you will have 48 hours to [Claim] your winnings, after that your ticket will be void and the winnings forfit. You can Check your active [Tickets]. Would you like to [Play Lotto]? Note: Lotto commands may be used via /tell as well.
/return

Sub Event_Tickets(Line, Player)
	/if (${Debug}) /echo in Tickets(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/call ReadINIVar ${TicketFile} ${Player}
	/delay ${TellDelay}
	/if (${Macro.Return.Equal[NULL]}) {
		/tell ${Player} You currently have no active tickets.
	} else {
		/tell ${Player} ${Macro.Return}
	}
/return

Sub Event_Previous(Line, Player)
	/if (${Debug}) /echo in Previous(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/call ReadINIVar ${BankFile} ${LottoSection} Previous
	/delay ${TellDelay}
	/tell ${Player} The Previous winning number was: ${Macro.Return}. If you won, please come [Claim] your 100k winnings!
/return

Sub Event_Current(Line, Player)
	/if (${Debug}) /echo in Current(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/call ReadINIVar ${BankFile} ${LottoSection} Current
	/delay ${TellDelay}
	/tell ${Player} The Current winning number is: ${Macro.Return}. If you won, please come [Claim] your 100k winnings!
/return
|||---------------------------------------------------------------------------------------------------------Lotto
|||Do Lotto Drawing
Sub CasinoLotto(Player, int RollMin, int RollMax, int Roll)
		/if (${Debug}) /echo in CasinoLotto(${Player}, RollMin ${RollMin}, RollMax ${RollMax}, Roll ${Roll})
		|||Move the numbers back a day
		/call ReadINIVar ${BankFile} ${LottoSection} Previous
		/declare Previous string local ${Macro.Return}
		/call ReadINIVar ${BankFile} ${LottoSection} Current
		/declare Current string local ${Macro.Return}
		/declare NewWinningTicket string local ${Roll}
		/call WriteINIVar ${BankFile} ${LottoSection} Old ${Previous}
		/call WriteINIVar ${BankFile} ${LottoSection} Previous ${Current}
		/call WriteINIVar ${BankFile} ${LottoSection} Current ${Roll}--${Time.Date}
		|||Announce the new winning ticket
		/delay ${TellDelay}
		/ooc Todays daily 100k jackpot goes to the holder of ticket #${Roll}! Check your [Tickets], if you have this ticket please come collect your winnings at MB in pok using [Claim].
		|||TODO Purge all tickets from more than 2 days ago from the ini
/return

Sub Event_Playlotto(Line, Player)
	/if (${Debug}) /echo in PlayLotto(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/if (!${Defined[${Player}]}) {
		/call BankedCheck ${Player}
		/if (!${Macro.Return}) /return
	}
	|||If player is still at blackjack or roulette table, don't let them switch games.
	/if (${${Player}[2]} == 3 || ${${Player}[2]} == 4) {
		/say ${Player}, Please wait until you are removed from your current table before starting a new game.
		/return
	}
	
	/call SetPlayerStatus ${Player} NONE 1 NONE
	/delay ${TellDelay}
	/tell ${Player} How many lotto tickets would you like to [buy #]?
/return

Sub Event_BuyTickets(Line, Player, int Tickets)
	/if (${Debug}) /echo in BuyTickets(${Player}, Tickets ${Tickets})
	/varset Player ${Player.Right[-2].Left[-1]}
|||If player did not do play lotto first, end transaction
	/if (${${Player}[2]} != 1) /return
	/declare Cost int local 0
	/varcalc Cost ${Tickets}*100
|||If player is buying more then he can afford, end transaction
	/if (${${Player}[1]} < ${Cost}) {
		/delay ${TellDelay}
		/tell ${Player} It will cost ${Cost} to buy ${Tickets} Tickets. Your current balance is: ${${Player}[1]}
		/return
	}
	
|||Roll the random ticket numbers for each ticket and write it to the ticket file
	/declare TicketNumbers[${Tickets}] string local 0
	/declare i int local
	/for i 1 to ${Tickets}
	:tryticketagain
		/varset TicketNumbers[${i}] ${Math.Rand[2001]}
		
		/if (${Ini[${TicketFile},${Player},${TicketNumbers[${i}]}].Length}) {
			/goto :tryticketagain
		}
		
		/call WriteINIVar ${TicketFile} ${Player} ${TicketNumbers[${i}]} ${Time.Date}
	/next i
	
|||Charge the player 
	/call PlayerLoses ${Player} ${Cost}
	/delay ${TellDelay}
	/tell ${Player} ${Tickets} tickets have successfully been processed for ${Cost}pp. You can check your [Tickets] at any time. Your remaining balance is ${${Player}[1]}
/return

Sub Event_Claim(Line, Player)
	/if (${Debug}) /echo in Claim(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/delay ${TellDelay}
	/tell ${Player} One moment while the casino checks your ticket and banks your winnings. Use [cashout] to transfer your winnings out of the casino bank.
/return
